const log = require('../util/log'),
  chromeMng = require('../mng/chromeMng.20180824')

module.exports = (req, res) => {
  require('../util/service').handlePost(req, res, async (reqObj, resolve) => {
	const mongoMng = require('../mng/mongoMng')
    if (!mongoMng.db) {
      log('mongodb未连接')
      resolve({
        result: -1,
        msg: '系统错误'
      })
      return
    }
    if (!reqObj.link) {
      resolve({
        result: -1,
        msg: '缺少link'
      })
      return
    }
    if (!chromeMng.client) {
      log('chrome未连接')
      resolve({
        result: -1,
        msg: '系统错误'
      })
      return
    }
    var data = [{
      customer_phone: req.session.customer.phone,
      link: reqObj.link,
      time: require('../../lib/formatDate')(new Date())
    }]
    mongoMng.db.collection('log').insert(data, (err, result) => {
      if (err) {
        log(err, data);
        return;
      }
    })
    mongoMng.db.collection('item').find({
      link: reqObj.link
    }).toArray((err, result) => {
      if (err) {
        log(err);
        resolve({
          result: -1,
          msg: '系统错误'
        })
        return;
      }
      if (result.length > 0 && result[0].advLink) {
        if (result[0].advLink == -1) {
          resolve({
            result: -1,
            msg: '该商品不能返利'
          })
          return
        }
        resolve({
          link: result[0].advLink
        })
        return
      }
      getTaobaoAdvLink(reqObj.link).then(result => {
        resolve({
          link: result
        })
      }, result => {
        resolve({
          result: -1,
          msg: result
        })
      })
    })
  })
}

function getTaobaoAdvLink(link) {
  if (link.indexOf('taobao') == -1 && link.indexOf('tmall') == -1 && link.indexOf('jd') == -1) {
    return new Promise((resolve, reject) => {
      reject('链接不合法')
    })
  }
  if (chromeMng.busy) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        getTaobaoAdvLink(link).then(result => {
          resolve(result)
        }, result => {
          reject(result)
        })
      }, 1000)
    })
  }
  chromeMng.busy = true
  return new Promise((resolve, reject) => {
    var getAdvLink = chromeMng.getTaobaoAdvLink
    if (link.indexOf('jd') != -1) {
      getAdvLink = chromeMng.getJdAdvLink
      // reject('暂时关闭京东')
      // return
    }
    getAdvLink({
      link: link
    }).then(result => {
      chromeMng.busy = false
      resolve(result)
      mongoMng.db.collection('item').find({
        link: link,
      }).toArray((err, _result) => {
        if (err) {
          log(err);
          return;
        }
        if (_result.length == 0) {
          mongoMng.db.collection('item').insert({
            link: link,
            advLink: result
          }, (err, result) => {
            if (err) {
              log(err);
              return;
            }
          })
        } else {
          mongoMng.db.collection('item').update({
            link: link
          }, {
            $set: {
              advLink: result
            }
          }, (err, result) => {
            if (err) {
              log(err);
              return;
            }
          })
        }
      });
    }, result => {
      log(result)
      chromeMng.busy = false
      reject(result)
      if (result == '该商品不能返利') {
        mongoMng.db.collection('item').find({
          link: link,
        }).toArray((err, _result) => {
          if (err) {
            log(err);
            return;
          }
          if (_result.length == 0) {
            mongoMng.db.collection('item').insert({
              link: link,
              advLink: -1
            }, (err, result) => {
              if (err) {
                log(err);
                return;
              }
            })
          } else {
            mongoMng.db.collection('item').update({
              link: link
            }, {
              $set: {
                advLink: -1
              }
            }, (err, result) => {
              if (err) {
                log(err);
                return;
              }
            })
          }
        });
      }
    })
  })
}
